<template>
  <!-- 反应釜管理 -->
  <div class="technologyReactionKettle">
    <basic-container>

      <!-- 表格内容 -->
      <avue-crud 
        :option="option"
        :table-loading="loading"
        :data="data"
        :page.sync="page"
        :permission="permissionList"
        :before-open="beforeOpen"
        v-model="form"
        ref="crud"
        @row-update="rowUpdate"
        @row-save="rowSave"
        @row-del="rowDel"
        @search-change="searchChange"
        @search-reset="searchReset"
        @selection-change="selectionChange"
        @current-change="currentChange"
        @size-change="sizeChange"
        @refresh-change="refreshChange"
        @on-load="onLoad"
      >
        <template slot="menuLeft">
          <el-button 
            type="danger"
            size="small"
            icon="el-icon-delete"
            plain
            v-if="permission.reactionkettle_delete"
            @click="handleDelete">
            删 除
          </el-button>
        </template>

        <template slot="idsLabel">
          <span style="color:#F56C6C">*</span><span>关联DCS:</span>
        </template>
        <template slot="idsForm">
          <el-form label-width="0px">
            <el-button 
              type="success"
              size="small"
              @click="handleDcs">
              选择 DCS
            </el-button>
            <span v-if="dcsTagsErr" class="dsc_error">请选择DCS数据点</span>
            <div class="dcs_point_list">
              <el-tag
                :key="index"
                v-for="(tag, index) in dcsTags"
                closable
                :disable-transitions="false"
                @close="closeDcsTag(index)">
                {{tag.dwbq}}
              </el-tag>
            </div>
          </el-form>
        </template>

      </avue-crud>

      <!-- 关联DCS弹窗 -->
      <el-dialog
        title="选择 DCS"
        :visible.sync="dialogDcs"
        append-to-body 
        @close='closeDialog'
        width="1200px"
      >
        <avue-crud 
          :option="dcsOption"
          :table-loading="dcsLoading"
          :data="dcsData"
          :page.sync="dcsPage"
          ref="dcsCrud"
          @search-change="dcsSearchChange"
          @search-reset="dcsSearchReset"
          @selection-change="dcsSelectionChange"
          @current-change="dcsCurrentChange"
          @size-change="dcsSizeChange"
        >
          <!-- 自定义状态 -->
          <template slot="zt" slot-scope="scope">
            <div v-if="scope.row['zt'] === 1">在线</div>
            <div v-else>下线</div>
          </template>
        </avue-crud>

        <span slot="footer" class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="chooseDcs()">确 定</el-button>
        </span>
      </el-dialog>

    </basic-container>
  </div>
</template>

<script>
import {getList, getDetail, add, update, remove, getDCSList} from "@/api/configdb/technologyReactionKettle";
import {mapGetters} from "vuex";

export default {
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        refreshBtn: false,
        columnBtn: false,
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        viewBtn: true,
        selection: true,
        dialogClickModal: false,
        column: [
          {
            label: "反应釜编码",
            prop: "fyfbh",
            align: "center",
            search: true,
            rules: [{
              required: true,
              message: "请输入反应釜编码",
              trigger: "blur"
            }]
          },
          {
            label: "反应釜名称",
            prop: "fyfmc",
            align: "center",
            search: true,
            rules: [{
              required: true,
              message: "请输入反应釜名称",
              trigger: "blur"
            }]
          },
          {
            label: "DCS点",
            prop: "dwbq",
            align: "center",
            addDisplay: false,
            editDisplay: false
          },
          {
            label: "关联DCS",
            prop: "ids",
            align: "center",
            hide: true,
            viewDisplay: false,
            formslot:true,
            labelslot:true
          }
        ]
      },
      data: [],
      dialogDcs: false,
      dcsQuery: {},
      dcsLoading: true,
      dcsPage: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      dcsSelectionList: [],
      dcsOption: {
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        menu: false,
        addBtn: false,
        viewBtn: true,
        selection: true,
        dialogClickModal: false,
        refreshBtn: false,
        columnBtn: false,
        column: [
          {
            label: "点位标签",
            prop: "dwbq",
            align: "center",
            search: true,
            rules: [{
              required: true,
              message: "请输入点位标签",
              trigger: "blur"
            }]
          },
          {
            label: "点位名称",
            prop: "dwmc",
            align: "center",
            search: true,
            rules: [{
              required: true,
              message: "请输入点位名称",
              trigger: "blur"
            }]
          },
          {
            label: "点位类型",
            prop: "dwlx",
            align: "center",
            type: 'select',
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=dcs_data_site_type",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            rules: [{
              required: true,
              message: "请选择点位类型",
              trigger: "blur"
            }]
          },
          {
            label: "最大值",
            prop: "zdfz",
            align: "center",
            type: "number",
            rules: [{
              required: true,
              message: "请输入最大值",
              trigger: "blur"
            }]
          },
          {
            label: "最小值",
            prop: "zxfz",
            align: "center",
            type: "number",
            rules: [{
              required: true,
              message: "请输入最小值",
              trigger: "blur"
            }]
          },
          {
            label: "单位",
            prop: "dw",
            align: "center",
            rules: [{
              required: true,
              message: "请输入单位",
              trigger: "blur"
            }]
          },
          {
            label: "状态",
            prop: "zt",
            align: "center",
            slot: true,
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false
          }
        ]
      },
      dcsData: [],
      dcsTags: [],
      dcsTagsErr: false,
      selectDcs: {}
    };
  },
  computed: {
    ...mapGetters(["permission"]),

    // 表格按钮权限
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.reactionkettle_add, false),
        viewBtn: this.vaildData(this.permission.reactionkettle_view, false),
        delBtn: this.vaildData(this.permission.reactionkettle_delete, false),
        editBtn: this.vaildData(this.permission.reactionkettle_edit, false)
      };
    },

    // 勾选项id
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {

    // 打开选择DCS弹窗
    handleDcs() {
      this.dialogDcs = true;
      this.dcsOnLoad(this.dcsPage);
    },

    // 关闭选择DCS弹窗
    closeDialog() {
      this.dcsPage.currentPage = 1;
      this.dialogDcs = false;
      this.dcsSelectionClear();
    },

    // DCS获取表格数据
    dcsOnLoad(page, params = {}) {
      this.dcsLoading = true;
      getDCSList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.dcsPage.total = data.total;
        this.dcsData = data.records;
        this.dcsLoading = false;
        if(this.selectDcs !== undefined && JSON.stringify(this.selectDcs) !== "{}") {
          this.$nextTick(() => {
          this.dcsData.forEach(row => {
            if(this.selectDcs[row.id] !== undefined) {
              this.$refs.dcsCrud.toggleRowSelection(row, true);
            }
          })
        })
        }
      });
    },

    // DCS清空勾选
    dcsSelectionClear() {
      this.dcsSelectionList = [];
      this.$refs.dcsCrud.toggleSelection();
    },

    // DCS搜索条件改变事件
    dcsSearchChange(params, done) {
      this.dcsQuery = params;
      this.dcsPage.currentPage = 1;
      this.dcsOnLoad(this.dcsPage, params);
      done();
    },

    // DCS清空搜索条件
    dcsSearchReset() {
      this.dcsQuery = {};
      this.dcsOnLoad(this.dcsPage);
    },

    // DCS勾选
    dcsSelectionChange(list) {
      this.dcsSelectionList = list;
    },

    // DCS换页数
    dcsCurrentChange(currentPage){
      var pageSelectIds = [];
      var pageAllIds = [];
      this.dcsSelectionList.forEach(item => {
        this.selectDcs[item.id] = item;
        pageSelectIds.push(item.id);
      });
      this.dcsData.forEach(item => {
        pageAllIds.push(item.id);
      });
      for(var id in this.selectDcs) {
        if(pageSelectIds.indexOf(id) < 0 && pageAllIds.indexOf(id) >= 0) {
          delete this.selectDcs[id];
        }
      }
      this.dcsPage.currentPage = currentPage;
      this.dcsOnLoad(this.dcsPage);
    },

    // DCS换每页显示数量
    dcsSizeChange(pageSize){
      this.dcsPage.pageSize = pageSize;
      this.dcsOnLoad(this.dcsPage);
    },

    // 选择DCS
    chooseDcs() {
      var pageSelectIds = [];
      var pageAllIds = [];
      this.dcsSelectionList.forEach(item => {
        this.selectDcs[item.id] = item;
        pageSelectIds.push(item.id);
      });
      this.dcsData.forEach(item => {
        pageAllIds.push(item.id);
      });
      for(let id in this.selectDcs) {
        if(pageSelectIds.indexOf(id) < 0 && pageAllIds.indexOf(id) >= 0) {
          delete this.selectDcs[id];
        }
      }
      if(this.selectDcs === undefined || JSON.stringify(this.selectDcs) === "{}") {
        this.$message.warning("请选择DCS数据点");
        return;
      }
      this.dcsTagsErr = false;
      this.dcsTags = [];
      for(let id in this.selectDcs) {
        this.dcsTags.push(this.selectDcs[id]);
      }
      this.closeDialog();
    },

    // 删除某一个dcs标签
    closeDcsTag(index) {
      this.dcsTags.splice(index, 1);
      if(this.dcsTags.length === 0) {
        this.dcsTagsErr = true;
      }
    },
    
    // 新增
    rowSave(row, done, loading) {
      if(this.dcsTags.length === 0) {
        this.dcsTagsErr = true;
        return;
      }
      delete row.dwbq;
      var idsArr = [];
      this.dcsTags.forEach(item => {
        idsArr.push(item.id);
      });
      row.ids = idsArr.join(",");
      add(row).then(() => {
        this.onLoad(this.page);
        this.$message.success("操作成功!");
        done();
      }, error => {
        loading();
        window.console.log(error);
      });
    },

    // 编辑
    rowUpdate(row, index, done) {
      if(this.dcsTags.length === 0) {
        this.dcsTagsErr = true;
        return;
      }
      var idsArr = [];
      var updateData = {
        id: row.id,
        fyfbh: row.fyfbh,
        fyfmc: row.fyfmc,
        ids: ""
      };
      this.dcsTags.forEach(item => {
        idsArr.push(item.id);
      });
      updateData.ids = idsArr.join(",");
      update(updateData).then(() => {
        this.onLoad(this.page);
        this.$message.success("操作成功!");
        done();
      });
    },

    // 删除一条
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return remove(row.id);
      }).then(() => {
        this.onLoad(this.page);
        this.$message.success("操作成功!");
      });
    },

    // 删除多条
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return remove(this.ids);
      }).then(() => {
        this.onLoad(this.page);
        this.$message.success("操作成功!");
        this.$refs.crud.toggleSelection();
      });
    },

    // 打开弹窗前
    beforeOpen(done, type) {
      this.dcsTagsErr = false;
      this.dcsTags = [];
      this.selectDcs = {};
      if (type === "edit") {
        getDetail(this.form.id).then(res => {
          this.form = res.data.data;
          this.form.dcsDataSiteInfos.forEach(item => {
            this.dcsTags.push(item);
            this.selectDcs[item.id] = item;
          });
        });
      }
      done();
    },

    // 清空搜索条件
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },

    // 搜索条件改变事件
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },

    // 勾选
    selectionChange(list) {
      this.selectionList = list;
    },

    // 清空勾选
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },

    // 换页数
    currentChange(currentPage){
      this.page.currentPage = currentPage;
      this.onLoad(this.page);
    },

    // 换每页显示数量
    sizeChange(pageSize){
      this.page.pageSize = pageSize;
      this.onLoad(this.page);
    },

    // 刷新
    refreshChange() {
      this.onLoad(this.page, this.query);
    },

    // 获取表格数据
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
    
  }
};
</script>

<style lang="scss">
.technologyReactionKettle {
  .el-form-item__label {
    width: 86px !important;
  }

  .avue-form__row {
    .el-form-item__content {
      margin-left: 86px !important;
    }
  }
}
</style>

<style lang="scss" scpoed>
.avue-crud__dialog {
  .el-dialog__body {
    .el-form-item__label {
      width: 95px !important;
    }

    .avue-form__row {
      .el-form-item__content {
        margin-left: 95px !important;
      }
    }
  }
} 

.el-dialog__wrapper {
  .el-dialog__body {
    padding: 10px 20px 20px 20px;

    .el-form-item__label {
      width: 72px;
    }

    .avue-form__row {
      .el-form-item__content {
        margin-left: 72px;
      }
    }

    .avue-crud__menu {
      display: none;
    }
  }
}

.dsc_error {
  color: #F56C6C; 
  font-size: 12px;
  margin-left: 5px;
}

.dcs_point_list {
  margin-top: 5px;

  .el-tag {
    margin-right: 5px;
  }
}
</style>